/*
 * @# uploadPhotos.js 2011-12-28 下午03:19:12
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */

var UploadPhotosDialog = {
	_haveInited : false,
	_$dialog : $('#dialog-upload-photos'),
	_createType : false,
	_createCallback : false,
	_swfu : null,
	_dataHaveShowed : false,
	
	_init : function() {
		this._haveInited = true;
		
		var self = this;
		
		this._$dialog.dialog({
			modal : true,
			title : '上传相片',
			autoOpen : false,
			width : 950,
			height : 500,
			resizable : false
		});
		this._swfu = new SWFUpload({
			upload_url : false,
			file_post_name : "photos", 
	        flash_url : window.cdn + "/js/swfupload/swfupload.swf", 
	        flash9_url : window.cdn + "/js/swfupload/swfupload_fp9.swf",
	        file_size_limit : "100 MB",
			file_types : "*.jpg;*.png;*.gif",
			file_types_description : "Image Files",
			file_upload_limit : 50,
			file_queue_limit : 0,
			
			debug: false,
			
			// Button settings
			button_image_url: window.cdn + "/images/main/upload.png",
			button_width: "61",
			button_height: "22",
			button_placeholder_id: "dialog-swfu-btn",
			
			file_dialog_complete_handler : function fileDialogComplete(numFilesSelected, numFilesQueued) {
				if (this.getStats().files_queued > 0) {
					$('#dialog-upload-photos-submit').prop('disabled', false);
				}
				var url = window.ctx + '/albums/' + $('#dialog-upload-albums-list').val() 
						+ '/photos/upload';
				this.setUploadURL(url);
				this.startUpload();
			},
			upload_success_handler : self._uploadSuccess
		});
		this._bindEvents();
	},
	
	_uploadSuccess : function(file, serverData) {
		try {
			var photos = jsonParse(serverData);
			var $divimg = $("#dialog-upload-datas li:not(:has(img))");
			if(!UploadPhotosDialog._dataHaveShowed){
				$('#dailog-upload-tips').hide();
				$('#dialog-upload-datas').show();
				$('#dialog-upload-photos-continue').show();
				
				UploadPhotosDialog._dataHaveShowed = true;
			}
			$.each(photos, function(index, photo) {
				$divimg.eq(index).append('<img src=' + window.imgUrl + photo.h160Path + ' alt="" />');
			});
		} catch (ex) {
			this.debug(ex);
		}
	},
	
	_bindEvents : function() {
		var self = this;
		
		$('#dialog-upload-albums-btn').click(function(e) {
			EdibAlbumDialog.createShows(function(album) {
				$('#dialog-upload-albums-list').prepend('<option value="' + album.id + '" selected>' + album.name + '</option>');
			});
		});
		
		$("#dialog-upload-datas li").hover(function(e) {
			if ($(this).is(':has(img)')) {
				$('.delPic', this).show();
			}
		}, function(e) {
			$('.delPic', this).hide();
		});
		$("#dialog-upload-datas li .delThis").click(function(){
			
		});
		
		$('#dialog-upload-photos-submit').click(function(e) {
			self._createCallback && self._createCallback();
		});
		$('#dialog-upload-photos-cancel').click(function(e) {
			self.close();
		});
		$('#dialog-upload-photos-continue').click(function(e) {
			self._swfu.selectFile();
			return false;
		});
	},
	
	createShows : function(album, callback) {
		this._create('shows', album, callback);
	},
	createDress : function(album, callback) {
		this._create('dress', album, callback);
	},
	
	_create : function(type, album, callback) {
		if ($.isFunction(album)) {
			callback = album;
			album = null;
		}
		this._createType = type;
		this._createCallback = callback;
		
		if (null == album) {
			$.getJSON(window.ctx + '/albums/list', function(albums) {
				var options = '';
				$.each(albums, function(index, album) {
					options += '<option value="' + album.id + '">' + album.name + '</option>';
				});
				$('#dialog-upload-albums-list').html(options);
			});	
			
			this._open();
		} else {
			$('#dialog-upload-albums-list-wrapper').hide();
			$('#dialog-upload-albums-list').html('<option value="' + album.id + '">' + album.name + '</option>');
		
			this._open(album.name);
		}
	},
	
	_open : function(title) {
		if (!this._haveInited) {
			this._init();
		}
		$('#dailog-upload-tips').show();
		$('#dialog-upload-datas').hide();
		$('#dialog-upload-photos-continue').hide();
		$("#dialog-upload-datas li img").remove();
		
		UploadPhotosDialog._dataHaveShowed = false;
		
		this._$dialog.dialog('open').dialog("option", "title", title || '上传相片');
	},
	
	close : function() {
		this._$dialog.dialog('close');
	}
};